package controle;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;


import modelo.Atendimento;
import modelo.Cliente;
import modelo.Colaborador;
import modelo.Ramo;
import modelo.Setor;
import modelo.Status;
import modelo.Tipo;



public class AgenciaDao {
	
	EntityManagerFactory entityManagerFactory;
	EntityManager entityManager;
	
	
	public AgenciaDao() {
		entityManagerFactory = Persistence.createEntityManagerFactory("agencia");
		entityManager = entityManagerFactory.createEntityManager();
	}
	
	@SuppressWarnings("unchecked")
	public List<Atendimento> listarAtendimento() {
		entityManager.getTransaction().begin();
		Query consulta = entityManager
				.createQuery("select atendimento from Atendimento atendimento");
		List<Atendimento> atendimentos = consulta.getResultList();
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
		return atendimentos;
	}
	
	@SuppressWarnings("unchecked")
	public List<Atendimento> listarAtendimentoPorCliente() {
		entityManager.getTransaction().begin();
		Query consulta = entityManager
				.createQuery("select atendimento from Atendimento atendimento, IN(atendimento.cliente) as clientes order by clientes.nomeFantasia");
		List<Atendimento> atendimentos = consulta.getResultList();
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
		return atendimentos;
	}
	
	@SuppressWarnings("unchecked")
	public List<Atendimento> listarAtendimentoPorColaborador() {
		entityManager.getTransaction().begin();
		Query consulta = entityManager
				.createQuery("select atendimento from Atendimento atendimento, IN(atendimento.colaborador) as colaboradores order by colaboradores.nomeColaborador");
		List<Atendimento> atendimentos = consulta.getResultList();
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
		return atendimentos;
	}
	
	public List<Tipo> listarTipoOrdenadoPorNome()
	{
		entityManager.getTransaction().begin();
		Query consulta = entityManager
				.createQuery("select tipo from Tipo tipo order by tipo.nomeTipo");
		@SuppressWarnings("unchecked")
		List<Tipo> tipo = consulta.getResultList();
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
		return tipo;
	}
	
	public List<Status> listarStatusOrdenadoPorNome()
	{
		entityManager.getTransaction().begin();
		Query consulta = entityManager
				.createQuery("select status from Status status order by status.descricaoStatus");
		@SuppressWarnings("unchecked")
		List<Status> status = consulta.getResultList();
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
		return status;
	}
	
	public Ramo buscarPorNomeRamo(String paramRamo) {
		entityManager.getTransaction().begin();
		String query = "SELECT ramo FROM Ramo ramo WHERE ramo.descricaoRamo  = :paramRamo";
		TypedQuery<Ramo> query1 = entityManager.createQuery(query,
				Ramo.class);
		query1.setParameter("paramRamo", paramRamo);
		Ramo ramo = query1.getSingleResult();
		entityManager.getTransaction().commit();
		// entityManagerFactory.close();
		entityManager.clear();

		return ramo;
	}
	
	public Setor buscarPorNomeSetor(String nomeSetor) {
		entityManager.getTransaction().begin();
		String query = "SELECT setor FROM Setor setor WHERE setor.nomeSetor  = :nomeSetor";
		TypedQuery<Setor> query1 = entityManager.createQuery(query,
				Setor.class);
		query1.setParameter("nomeSetor", nomeSetor);
		Setor setor = query1.getSingleResult();
		entityManager.getTransaction().commit();
		// entityManagerFactory.close();
		entityManager.clear();

		return setor;
	}

	
	public void inserirRamo(Ramo paramRamo) {
		entityManager.getTransaction().begin();
		entityManager.persist(paramRamo);
		entityManager.getTransaction().commit();
		entityManager.clear();
		entityManager.close();
		entityManagerFactory.close();
	}
	
	public void inserirCliente(Cliente paramCliente) {
		entityManager.getTransaction().begin();
		entityManager.persist(paramCliente);
		entityManager.getTransaction().commit();
		entityManager.clear();
		entityManager.close();
		entityManagerFactory.close();
	}
	
	public void inserirTipo(Tipo paramTipo) {
		entityManager.getTransaction().begin();
		entityManager.persist(paramTipo);
		entityManager.getTransaction().commit();
		entityManager.clear();
		entityManager.close();
		entityManagerFactory.close();
	}
	
	public void inserirSetor(Setor paramSetor) {
		entityManager.getTransaction().begin();
		entityManager.persist(paramSetor);
		entityManager.getTransaction().commit();
		entityManager.clear();
		entityManager.close();
		entityManagerFactory.close();
	}
	
	public void inserirStatus(Status paramStatus) {
		entityManager.getTransaction().begin();
		entityManager.persist(paramStatus);
		entityManager.getTransaction().commit();
		entityManager.clear();
		entityManager.close();
		entityManagerFactory.close();
	}
	
	public void inserirColaborador(Colaborador paramColaborador) {
		entityManager.getTransaction().begin();
		entityManager.persist(paramColaborador);
		entityManager.getTransaction().commit();
		entityManager.clear();
		entityManager.close();
		entityManagerFactory.close();
	}
	
	public void excluirRamo(Ramo paramRamo) {
		entityManager.getTransaction().begin();
		entityManager.remove(paramRamo);
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
	}
	
	public List<Setor> listarSetorOrdenadoPorNome()
	{
		entityManager.getTransaction().begin();
		Query consulta = entityManager
				.createQuery("select setor from Setor setor order by setor.nomeSetor");
		@SuppressWarnings("unchecked")
		List<Setor> setor = consulta.getResultList();
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
		return setor;
	}
	
	public List<Ramo> listarRamoOrdenadoPorNome()
	{
		entityManager.getTransaction().begin();
		Query consulta = entityManager
				.createQuery("select ramo from Ramo ramo order by ramo.descricaoRamo");
		@SuppressWarnings("unchecked")
		List<Ramo> ramo = consulta.getResultList();
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
		return ramo;
	}

	public Cliente buscarPorNomeCliente(String nomeFantasia) {
		entityManager.getTransaction().begin();
		String query = "SELECT cliente FROM Cliente cliente WHERE cliente.nomeFantasia  = :nomeFantasia)";
		TypedQuery<Cliente> query1 = entityManager.createQuery(query,
				 Cliente.class);
		query1.setParameter("nomeFantasia", nomeFantasia);
		 Cliente cliente = query1.getSingleResult();
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
		return cliente;
	}
	
	public Colaborador buscarPorNomeColaborador(String nomeColaborador) {
		entityManager.getTransaction().begin();
		String query = "SELECT colaborador FROM Colaborador colaborador WHERE colaborador.nomeColaborador  = :nomeColaborador";
		TypedQuery<Colaborador> query1 = entityManager.createQuery(query,
				Colaborador.class);
		query1.setParameter("nomeColaborador", nomeColaborador);
		Colaborador colaborador = query1.getSingleResult();
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
		return colaborador;
	}
	
	public List<Cliente> listarClienteOrdenadoNome() {
		entityManager.getTransaction().begin();
		Query consulta = entityManager
				.createQuery("select cliente from Cliente cliente order by cliente.nomeFantasia");
		@SuppressWarnings("unchecked")
		List<Cliente> cliente = consulta.getResultList();
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
		return cliente;
	}
	
	public List<Colaborador> listarColaboradorOrdenadoNome()
	{
		entityManager.getTransaction().begin();
		Query consulta = entityManager
				.createQuery("select colaborador from Colaborador colaborador order by colaborador.nomeColaborador");
		@SuppressWarnings("unchecked")
		List<Colaborador> colaborador = consulta.getResultList();
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
		return colaborador;
	}
	
	public void inserirAtendimento(Atendimento atendimento)
	{
		entityManager.getTransaction().begin();
		entityManager.persist(atendimento);
		entityManager.getTransaction().commit();
		entityManager.close();
		entityManagerFactory.close();
	}
}
